﻿using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace System.Management.HyperV.Tests
{
    /// <summary>
    /// Tests creation of Virtual Hard Disks (VHDs) of three types:
    ///     Dynamically Expanding
    ///     Fixed Size
    ///     Differencing
    /// </summary>
    [TestClass]
    public class ImageManagementServiceTest
    {
        private readonly string workingFolder =
            string.Format("{0}\\",
                Environment.GetFolderPath(
                    Environment.SpecialFolder.MyDocuments));

        [TestMethod]
        public void CreateDynamicallyExpandingDisk_Test()
        {
            string vhdFileName = workingFolder + "dynamically-expanding-test-disk.dyn.vhd";
            Assert.IsFalse(File.Exists(vhdFileName), "The name specified for the dynamically expanding VHD file is already in use.");

            using (var ims = new ImageManagementService())
            {
                try
                {
                    ulong vhdSize500MB = 500 * 1024 * 1024;
                    ims.CreateDynamicVirtualHardDisk(vhdFileName, vhdSize500MB);
                    Assert.IsTrue(File.Exists(vhdFileName), "The dynamically expanding VHD file does not exist (probably failed to be created).");

                    var vhdInfo = ims.GetVirtualHardDiskInfo(vhdFileName);
                    Assert.IsTrue(vhdInfo.DiskType == VirtualHardDiskType.Dynamic);
                    Assert.IsTrue(vhdInfo.MaxInternalSize == vhdSize500MB);
                    Assert.IsTrue(vhdInfo.ParentPath == string.Empty);
                }
                finally
                {
                    if (File.Exists(vhdFileName))
                    {
                        File.Delete(vhdFileName);
                    }
                }
            }
        }
        [TestMethod]
        public void CreateFixedSizedDisk_Test()
        {
            string vhdFileName = workingFolder + "fixed-sized-test-disk.vhd";
            Assert.IsFalse(File.Exists(vhdFileName), "The name specified for the fixed sized VHD file is already in use.");

            using (var ims = new ImageManagementService())
            {
                try
                {
                    ulong vhdSize50MB = 50 * 1024 * 1024;
                    ims.CreateFixedVirtualHardDisk(vhdFileName, vhdSize50MB);
                    Assert.IsTrue(File.Exists(vhdFileName), "The fixed sized VHD file does not exist (probably failed to be created).");

                    var vhdInfo = ims.GetVirtualHardDiskInfo(vhdFileName);
                    Assert.IsTrue(vhdInfo.DiskType == VirtualHardDiskType.Fixed);
                    Assert.IsTrue(vhdInfo.MaxInternalSize == vhdSize50MB);
                    Assert.IsTrue(vhdInfo.ParentPath == string.Empty);
                }
                finally
                {
                    if (File.Exists(vhdFileName))
                    {
                        File.Delete(vhdFileName);
                    }
                }
            }
        }
        [TestMethod]
        public void CreateDifferencingDisk_Test()
        {
            string differencingVhdFileName = workingFolder + "differencing-test-disk.diff.vhd";
            Assert.IsFalse(File.Exists(differencingVhdFileName), "The name specified for the differencing VHD file is already in use.");

            string dynamicallyExpandingVhdFileName = workingFolder + "dynamically-expanding-test-disk.dyn.vhd";
            Assert.IsFalse(File.Exists(dynamicallyExpandingVhdFileName), "The name specified for the fixed sized VHD file is already in use.");

            using (var ims = new ImageManagementService())
            {
                try
                {
                    ulong vhdSize500MB = 500 * 1024 * 1024;
                    ims.CreateDynamicVirtualHardDisk(dynamicallyExpandingVhdFileName, vhdSize500MB);
                    Assert.IsTrue(File.Exists(dynamicallyExpandingVhdFileName), "The dynamically expanding VHD file does not exist (probably failed to be created).");

                    ims.CreateDifferencingVirtualHardDisk(differencingVhdFileName, dynamicallyExpandingVhdFileName);
                    Assert.IsTrue(File.Exists(differencingVhdFileName), "The differencing VHD file does not exist (probably failed to be created).");

                    var vhdInfo = ims.GetVirtualHardDiskInfo(differencingVhdFileName);
                    Assert.IsTrue(vhdInfo.DiskType == VirtualHardDiskType.Differencing);
                    Assert.IsTrue(vhdInfo.ParentPath == dynamicallyExpandingVhdFileName);
                }
                finally
                {
                    if (File.Exists(dynamicallyExpandingVhdFileName))
                    {
                        File.Delete(differencingVhdFileName);
                    }
                    if (File.Exists(differencingVhdFileName))
                    {
                        File.Delete(dynamicallyExpandingVhdFileName);
                    }
                }
            }
        }
    }
}
